import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class CurrentSchedulePanel extends JPanel{

        JPanel basePanel = new JPanel();
        JPanel panel = new JPanel();
        JPanel panelt = new JPanel();
        JPanel panelc = new JPanel();
        JPanel panelb = new JPanel();
        JButton home = new JButton();
        String[] cols={"Term", "Course Title","Course Number", "Professor", "Meeting Info", "Credits"};
        Object data[][]={};
        
        DefaultTableModel model = new DefaultTableModel();
        JTable table;
        Image img;
        
        
        public CurrentSchedulePanel() throws SQLException{
               
        	img=new ImageIcon("currents_background.png").getImage();
                setLayout(new BorderLayout());
                add(panel,BorderLayout.PAGE_START);
                panel.setOpaque(false);
                setOpaque(false);
                
                panelt.setPreferredSize(new Dimension(600,35));
                panelt.setBorder(BorderFactory.createLineBorder(Color.white));
                panelt.setBackground(Color.WHITE);
        panelt.setLayout(new BorderLayout());
        JLabel cs=new JLabel(MenuPanel.fname+" "+MenuPanel.lname);
        JLabel name=new JLabel(MenuPanel.student_id);
        JPanel e=new JPanel();
        e.setPreferredSize(new Dimension(600,20));
        e.setOpaque(false);
       // panelt.setOpaque(false);
        //panelt.add(e,BorderLayout.NORTH);
        panelt.add(cs,BorderLayout.CENTER);
        panelt.add(name,BorderLayout.SOUTH);
        panel.add(panelt, BorderLayout.NORTH);
                
                
        
        panelc.setBackground(Color.WHITE);
        panelc.setLayout(new BorderLayout());
        panelc.setBorder(BorderFactory.createTitledBorder("Current Schedule"));
        panelc.setPreferredSize(new Dimension(600,300));
        panelc.setOpaque(false);
//        model.addColumn(cols[0]);
//        model.addColumn(cols[1]);
//        model.addColumn(cols[2]);
//        model.addColumn(cols[3]);
//        model.addColumn(cols[4]);
//        model.addColumn(cols[5]);
//        model.addRow(data[0]);
//        model.addRow(data[1]);
//        model.addRow(data[2]);

        initialize();
       
       // table = new JTable(model){
        //        public boolean isCellEditable(int row, int cols) {return false;}
       // };
                TableColumn column;
        column = table.getColumnModel().getColumn(0);
        column.setPreferredWidth(45);
                
        column = table.getColumnModel().getColumn(1);
        column.setPreferredWidth(135);
        
        column = table.getColumnModel().getColumn(2);
        column.setPreferredWidth(45);
                
        column = table.getColumnModel().getColumn(3);
        column.setPreferredWidth(50);
        
        column = table.getColumnModel().getColumn(4);
        column.setPreferredWidth(184);
        
        column = table.getColumnModel().getColumn(5);
        column.setPreferredWidth(24);
        
        panelc.add(table.getTableHeader(), BorderLayout.PAGE_START);
        panelc.add(table, BorderLayout.CENTER);
       
        panel.add(panelc,BorderLayout.CENTER);
                
                
                
                
                panelb.add(home,BorderLayout.SOUTH);
                panelb.setOpaque(false);
                panel.add(panelb, BorderLayout.SOUTH);
                panel.setPreferredSize(new Dimension(600,400));
                panel.setBackground(Color.WHITE);
                
                add(panel, BorderLayout.PAGE_START);
                
                final ImageIcon ho=new ImageIcon("home_button.png");
        		final ImageIcon cho=new ImageIcon("home_cbutton.png");
        		button(home,ho);
                
                home.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
                remove(panel);
                add(new MenuPanel(), BorderLayout.PAGE_START);
                validate();
                repaint();
                
            }
    });
                home.setContentAreaFilled(false);
                home.setIcon(ho);
                home.setRolloverIcon(cho);
                home.setPressedIcon(cho);
                home.getModel().addChangeListener(new ChangeListener() {
        		        @Override
        		        public void stateChanged(ChangeEvent e) {
        		            ButtonModel model = (ButtonModel) e.getSource();
        		            if (model.isRollover()) {
        		            	home.setIcon(cho);
        		            } else {
        		            	home.setIcon(ho);
        		            }// etc ...//  if (model.isPressed()) {
        		         }
        		    });
    }
        
        private void initialize(){
        	
        	DefaultTableModel dtm=new DefaultTableModel(data,cols){
    			public boolean isCellEditable(int nRow, int nCol) {
                    return false;
                }
    		};
    		table=new JTable(dtm);
    		
        	try{
    			String dName = "com.mysql.jdbc.Driver";
    			String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
    			Connection con = DriverManager.getConnection(url);
    			java.sql.Statement mysqlStatement = con.createStatement();
    			java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT cu.term, cu.start_time, cu.end_time, cu.days, cu.instructor, ca.course_name, ca.course_number, ca.credit_hours, cu.CID FROM current_course_total_list AS cu JOIN course_catalog AS ca ON cu.CID = ca.CID WHERE cu.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"') GROUP BY course_name");
    			
    			while (resultSet.next()) {
    				String term=resultSet.getString(1);
    				String start_time="";//=resultSet.getString(2);
    				String end_time="";//=resultSet.getString(3);
    				//String days;//=resultSet.getString(4);
    				String instructor=resultSet.getString(5);
    				String course_name=resultSet.getString(6);
    				String course_number=resultSet.getString(7);
    				
    				int credit_hours=resultSet.getInt(8);
    				String cid=resultSet.getString(9);
    				String days="";
    				
    				try{
    					String dName2 = "com.mysql.jdbc.Driver";
    					String url2 = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
    					Connection con2 = DriverManager.getConnection(url2);
    					java.sql.Statement mysqlStatement2 = con2.createStatement();
    					//java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT course_name, start_time, days From course_catalog, current_course_total_list WHERE course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
    					java.sql.ResultSet resultSet2 = mysqlStatement2.executeQuery("SELECT co.start_time, co.end_time, co.days, co.CID From course_catalog AS ca JOIN current_course_total_list AS co ON ca.CID = co.CID WHERE ca.CID ='"+cid+"' AND co.SID='"+MenuPanel.SID+"'");//start_time='"+start_time+"'");//course_catalog.CID IN (SELECT CID FROM current_course_total_list WHERE SID='"+MenuPanel.SID+"')");// AND current_course_total_list.SID='"+MenuPanel.SID+"')");
    					
    					//while (resultSet.next()&&resultSet2.next()) {
    						while(resultSet2.next()){
    						//String course_number= resultSet.getString(1);
    						//String course_section=resultSet.getString(3);
    						//course=course_number+"-"+course_name+"-"+course_section;
    						
    						String startTime=resultSet2.getString(1);
    						String endTime=resultSet2.getString(2);
    						String day=resultSet2.getString(3);
    						start_time=startTime;
    						end_time=endTime;
    						
    						//start_time+=startTime;
    						//end_time+=endTime;
    						days+=day;
    						
    						System.out.println(cid+": "+startTime+ " "+endTime+" "+days+" ");
    						
    						
    					}
    					con2.close();
    					}
    					catch(Exception ex){}
    				String dd=days+" "+start_time+":00 - "+end_time+":00";
    				
    				Object[] program={term,course_name,course_number,instructor,dd,credit_hours};
    				dtm.addRow(program);
    				System.out.println("added");
    			}
    			con.close();
    			}
    			catch(Exception ex){}
        }
        
        @Override
        public void paintComponent(Graphics g) {
    		super.paintComponents(g);
            g.drawImage(img, 0, 0, this);
        }
    	
    	private void button(JButton b, ImageIcon i){
    		b.setBorderPainted(false);
    		b.setBorder(null);
    		//button.setFocusable(false);
    		b.setMargin(new Insets(0, 0, 0, 0));
    		b.setContentAreaFilled(false);
    		b.setIcon(i);
    		//b.setRolloverIcon(myIcon2);
    		//b.setPressedIcon(myIcon3);
    	}
}
